package com.yjd.bi.user.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yjd.bi.report.model.EChartsModel;
import com.yjd.bi.report.util.TimeDimensionUtil;
import com.yjd.bi.user.dao.IUserStatisticsRealTimeDao;
import com.yjd.bi.user.model.UserStatisticsRealTimeModel;
import com.yjd.bi.user.service.IUserStatisticsRealTimeService;
import com.yjd.comm.base.dao.IBaseDao;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.base.service.impl.BaseServiceImpl;
import com.yjd.comm.util.DateUtil;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.NumberUtil;

/**
 * <b>description</b>：用户数实时统计业务实现<br>
 * <b>time</b>：2016-04-19 14:35:36 <br>
 * <b>author</b>： renbangjie renbangjie@126.com
 */
@Service("userStatisticsRealTimeService")
public class UserStatisticsRealTimeServiceImpl extends BaseServiceImpl
		implements IUserStatisticsRealTimeService {

	/**
	 * 插入
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public UserStatisticsRealTimeModel insert(UserStatisticsRealTimeModel model)
			throws Exception {
		return this.userStatisticsRealTimeDao.insertModel(model, true);
	}

	/**
	 * 更新
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public int update(UserStatisticsRealTimeModel model) throws Exception {
		return this.userStatisticsRealTimeDao.updateModel(model, true);
	}

	/**
	 * 根据id删除
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public int delete(long id) throws Exception {
		Map<Object, Object> paramMap = new HashMap<Object, Object>();
		paramMap.put("id", id);
		return this.userStatisticsRealTimeDao.deleteModel(paramMap, true);
	}

	@SuppressWarnings("unchecked")
	@Override
	public EChartsModel getDates(Long day, String field) throws Exception {
		EChartsModel ec = new EChartsModel();
		ec.setxAxis(FrameUtil.newArrayList(null));
		ec.setLegend(FrameUtil.newArrayList(DateUtil
				.getFormateStringByDailyDimension(day,
						DateUtil.PATTERN_yyyy_MM_dd)));
		ec.addSeries(FrameUtil.newArrayList(null), DateUtil
				.getFormateStringByDailyDimension(day,
						DateUtil.PATTERN_yyyy_MM_dd));
		List<UserStatisticsRealTimeModel> m = this.getModelList(FrameUtil
				.newHashMap("start", day.longValue(), "end", DateUtil
						.timestampToDateString(
								DateUtil.dateStringToTimestamp("" + day,
										"yyyyMMddHHmmss")
										+ DateUtil.DAY_IN_SECOND,
								"yyyyMMddHHmmss")), DbWREnums.READ);
		for (UserStatisticsRealTimeModel model : m) {
			ec.getSeries()
					.get(0)
					.getData()
					.add(NumberUtil.formatNumber(
							FrameUtil.getField(model, field),
							NumberUtil.FORMAT_1));
		}

		for (String d : TimeDimensionUtil.getDailyDemention()) {
			ec.getXAxis().add(d);
			if (ec.getSeries().get(0).getData().size() < ec.getXAxis().size()) {
				ec.getSeries().get(0).getData().add("0");
			}
		}

		return ec;
	}

	@Resource
	private IUserStatisticsRealTimeDao userStatisticsRealTimeDao;

	@Override
	public IBaseDao getBaseDao() {
		return this.userStatisticsRealTimeDao;
	}

}
